#include "GameStateMachine.h"
#include <iostream>

GameStateMachine::GameStateMachine()
{
	// do nothing
}

void GameStateMachine::pushState(GameState* pState)
{
	m_gameStates.push_back(pState);
	m_gameStates.back()->init();
}

void GameStateMachine::popState()
{
	if (!m_gameStates.empty())
	{
		if (m_gameStates.back()->init())
		{
			//delete m_gameStates.back();
			//GameState* tmp = m_gameStates.back();
			//delete tmp;
			if (!m_gameStates.empty())
			{
				std::cout << "not empty" << std::endl;
			}
			//m_gameStates.pop_back();
			if (m_gameStates.empty())
			{
				std::cout << "empty";
			}
			m_gameStates.pop_back();
			m_gameStates.back()->update();
		}
	}
}

void GameStateMachine::changeState(GameState *pState)
{
	//if (!m_gameStates.empty())
	//{	
	//	if (m_gameStates.back()->exit())
		//{
		//	m_gameStates.pop_back();
		//}
	//}
	//push back new state
	m_gameStates.push_back(pState);
	//initialize it
	m_gameStates.back()->init();
}

void GameStateMachine::update()
{
	if (!m_gameStates.empty())
	{
		m_gameStates.back()->update();
	}
}

void GameStateMachine::render()
{
	if (!m_gameStates.empty())
	{
		m_gameStates.back()->render();
	}
}